#include "REG52.H"
 sbit L1 = P1^7;
 sbit L2 = P1^6;
 sbit L3 = P1^5;
 sbit L4 = P1^4;
 sbit R1 = P1^3;
 sbit R2 = P1^2;
 sbit R3 = P1^1;
 sbit R4 = P1^0;
 sbit wmc = P2^4;
 sbit wmb = P2^3;
 sbit wma = P2^2;

void delay(int x)
{
    while(x--);
}
unsigned int arr[16] = {
                            0x3F, // 0
                            0x06, // 1
                            0x5B, // 2
                            0x4F, // 3
                            0x66, // 4
                            0x6D, // 5
                            0x7D, // 6
                            0x07, // 7  
                            0x7F, // 8
                            0x6F, // 9
                            0x77, // A
                            0x7C, // B
                            0x39, // C
                            0x5E, // D
                            0x79, // E
                            0x71  // F 
                        };
void which_key_press(int *pr,int *pc)
{
    while (1)
    {
        //检测行
        P1 = 0xf0;
        if (P1 != 0xf0)
        {
            delay(1000);
            if (P1 != 0xf0)
            {
                if ((P1&0xf0) == 0x70) *pr =1;
                if ((P1&0xf0) == 0xb0) *pr =2;
                if ((P1&0xf0) == 0xd0) *pr =3;
                if ((P1&0xf0) == 0xe0) *pr =4;
            }
            
        }
        //检测列
        P1 = 0x0f;
        if (P1 !=0x0f)
        {
            delay(1000);
            if (P1 != 0x0f)
            {
                if ((P1&0x0f) == 0x07) *pc =1;
                if ((P1&0x0f) == 0x0b) *pc =2;
                if ((P1&0x0f) == 0x0d) *pc =3;
                if ((P1&0x0f) == 0x0e) *pc =4;
            }
            
        }
 
            if (*pr > 0 && *pc > 0) 
            {
            break;
            }
    }

}
void main()
{
    int row = 0;
    int col = 0;
    int num = 0;
    int *pr = &row;
    int *pc = &col;
    while(1){
        which_key_press(pr,pc);
        num = (row-1)*4 + col;
        if (num >= 0 && num < 16) 
            { // 确保num在合法范围内
                P0 = arr[num]; // 显示对应数码管段码
            }
    }
}